<template>
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="2"
      @filterChange="changeHandle"
      labelWidth="120"
      :isSubmitBtn="false"
    ></TopFilter>
    <FilterTable
      ref="table"
      height="300"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
      :selectionType="'single'"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="addFn.bind()">新 增</multiuse-button>
        <multiuse-button size="small" type="primary" :click="findFn.bind()">搜 索</multiuse-button>
        <multiuse-button size="small"  :click="blankFn.bind()">重 置</multiuse-button>
      </template>
    </FilterTable>
    <FilterTable
      ref="tables"
      height="auto"
      :dataSource="tableList"
      :columns="columns1"
      :onColumnsChange="columns => this.columns1 = columns"
      :isSelectColumn="false"
    >

    </FilterTable>
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <el-button @click="closeDrawer(2)" type="primary" :disabled='tableList.length<1'>确 定</el-button>
    </div>
  </div>
</template>
<script>
import {getList,preSaveList,saveOne} from '@/api/claim/dealer/QualityInformationManagement_DLR/QualityInformationFeedback/index';
import { dictionary } from '@/utils/dictMixin';
export default {
  data(){
    this.tables=null;
    return{
      topFilterList:this.createdFrom(),
      columns:this.createdTable(),
      columns1:this.createdTables(),
      fetchParams:{

      },
      fetchApi:getList,
      selectList:[],
      tableList:[],
    }
  },
  mounted(){
    this.tables=this.$refs.tables.$refs.pageTable;
  },
  mixins:[dictionary],
  methods:{
    blankFn(){
      this.topFilterList.find(x => x.fieldName == 'fcNo').initialValue='';
      this.topFilterList.find(x => x.fieldName == 'fcName').initialValue='';
      let val = {fcNo:this.topFilterList.find(x => x.fieldName == 'fcNo').initialValue,fcName:this.topFilterList.find(x => x.fieldName == 'fcName').initialValue}
      this.changeHandle(val);
    },
    findFn(){
      let val = {fcNo:this.topFilterList.find(x => x.fieldName == 'fcNo').initialValue,fcName:this.topFilterList.find(x => x.fieldName == 'fcName').initialValue}
      this.changeHandle(val);
    },
    async addFn(){
      let saveData = {};
      saveData.fcNo = this.topFilterList.find(x => x.fieldName == 'fcNo').initialValue;
      saveData.fcName = this.topFilterList.find(x => x.fieldName == 'fcName').initialValue;
      if (!this.topFilterList.find(x => x.fieldName == 'fcNo').initialValue && !this.topFilterList.find(x => x.fieldName == 'fcName').initialValue) {
        this.$notify.warning({title: '提示信息', message: '请选择故障码和故障描述' || '保存失败!'});
      } else {
        let res1 = await preSaveList(saveData);
        if(res1.data){
          this.$confirm('此故障码已存在是否更新?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async() => {
              this.loading = true;
              let res = await saveOne(saveData);
              if (res.resultCode === 200) {
                this.changeHandle();
                return this.$notify.success({title: '提示信息', message: '保存成功'});
              } else {
                this.loading = false;
                // this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
                // this.$emit('save');
              }
            })
            .catch(() => {});
        }else{
          let res = await saveOne(saveData);
          console.log(res);
          if (res.resultCode === 200) {
            this.changeHandle();
            return this.$notify.success({title: '提示信息', message: '保存成功'});
          } else {
            this.loading = false;
            // this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
            // this.$emit('save');
          }
        }

      }
    },
    createdFrom(){
      return [
        {
          type: 'INPUT',
          label: '诊断仪检测故障码',
          fieldName: 'fcNo',
          placeholder: '请选择',
        },
        {
          type: 'INPUT',
          label: '故障描述',
          fieldName: 'fcName',
          placeholder: '请选择',
        },
      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '诊断仪检测故障码',
          dataIndex: 'fcNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '故障描述',
          dataIndex: 'fcName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ]
    },
    createdTables(){
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.delFun(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '诊断仪检测故障码',
          dataIndex: 'fcNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '故障描述',
          dataIndex: 'fcName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
      ]
    },
    changeHandle(val){
      this.fetchParams=Object.assign({},this.fetchParams,val)
    },
    delFun(data) {
      this.tables.EXECUTE_DELETE([data]);
    },
    closeDrawer(type){
      if(type==1){
        this.$emit('close')
      }else{
        let arr=[];
        this.tableList.map(x=>{
          arr.push({faultName:x.fcName,faultCode:x.fcNo})
        })
        this.$emit('save',arr)
      }
    },
    selectChangeHandle(val){
      if(val.length>0){
        let json=val[0];
        if(this.tableList.length>0){
          let is=false;
          this.tableList.map(x=>{
            if(json.fcNo==x.fcNo){
              is=true;
            }
          })
          if(!is){
            this.tables.EXECUTE_INSERT(val);
          }else{
            this.$notify({
              title: '警告',
              message: '请不要重复添加同一故障码',
              type: 'warning'
            });
          }
        }else{
          this.tables.EXECUTE_INSERT(val);
        }


      }

    }
  },

}
</script>
